<?php

namespace App\Http\Controllers\Develop;

use App\Models\User\Permission;
use App\Models\User\PermissionGroup;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis as Redis;

class PermissionController extends Controller {

    public function index() {

        $one_level = [];
        $two_level = [];

        //获取顶级权限组
        $group_data = PermissionGroup::where('parent_id',0)->orderBy('sort', 'asc')->get()->toArray();
        if ( !empty($group_data) ) {
            foreach ( $group_data as $item ) {
                $one_level[] = [
                    'group_id' => $item['group_id'],
                    'group_name' => $item['group_name']
                ];
            }
        }

        //获取子级权限组
        if ( !empty( $one_level ) ) {

            foreach ( $one_level as $item ) {

                $group_data = PermissionGroup::where('parent_id',$item['group_id'])->orderBy('sort', 'asc')->get()->toArray();
                if ( !empty( $group_data ) ) {
                    foreach ( $group_data as $v ) {
                        $two_level[$item['group_id']][] = [
                            'group_id' => $v['group_id'],
                            'group_name' => $v['group_name']
                        ];
                    }
                }

            }

        }

        return view('develop/permissionList' , [ 'one_level'=> json_encode($one_level) , 'two_level'=>json_encode($two_level)] );
    }


    public function search( Request $request ) {

        $where = [];

        if ( $request->input('one_level') ) {
            $where['permissionGroupID'] =  $request->input('one_level');
        }
        if ( $request->input('two_level') ) {
            $where['secondLevelPermissionGroupID'] =  $request->input('two_level');
        }
        if ( $request->input('permissionID') ) {
            $where['permissionID'] = $request->input('permissionID');
        }
        if ( $request->input('permissionName') ) {
            $where[] = ['permissionName' , 'LIKE' , '%'.$request->input('permissionName').'%'] ;
        }

        //查询用户信息
        $permission_data = Permission::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $res_data = array(
            'total' => $permission_data['total'],
            'rows' => array()
        );

        if ( $permission_data['data']  ) {
            foreach ( $permission_data['data'] as $item ) {

                $operate ='<a href="javascript: void(0);" onclick="Permission.del(' . $item['permissionID'] . ')">删除</a>&nbsp;&nbsp;
                            <a href="javascript: void(0);" onclick="Permission.open(' . $item['permissionID'] . ')">修改</a>&nbsp;&nbsp;';

                $res_data['rows'][] = array(
                    'operate' => $operate,
                    'permissionID' => $item['permissionID'],
                    'permissionName' => $item['permissionName'],
                    'permissionGroupName' => $item['permissionGroupName'],
                    'permissionRequest' => $item['permissionRequest']
                );

            }
        }

        return response()->json($res_data);

    }

    public function delete( $permissionID ) {

        if ( !isset( $permissionID ) || !ebsig_is_int( $permissionID ) ) {
            return response()->json([ 'code'=>10000, 'message'=>'参数错误']);
        }

        Permission::find($permissionID)->delete();

        return response()->json([ 'code'=>200, 'message'=>'删除成功']);

    }

    public function get( $permissionID ) {

        if ( !isset( $permissionID ) || !ebsig_is_int( $permissionID ) ) {
            return response()->json([ 'code'=>10000, 'message'=>'参数错误']);
        }

        $data = Permission::find($permissionID);

        if ( !$data ) {
            return response()->json([ 'code'=>10001, 'message'=>'权限不存在']);
        }

        $data = $data->toArray();

        return response()->json([ 'code'=>200, 'message'=>'ok' , 'data' => $data]);

    }

    //保存权限
    public function store( Request $request) {

        $args_data = $request->all();

        if (!isset($args_data['permissionID'], $args_data['permissionName'], $args_data['permissionGroupID'], $args_data['permissionRequest'])) {
            return response()->json([ 'code'=>100000, 'message'=>'参数错误']);
        }

        if (!isset($args_data['uuid']) || empty($args_data['uuid'])) {
            $args_data['uuid'] = null;
        }

        if (!ebsig_is_int($args_data['permissionID'])) {
            return response()->json([ 'code'=>100001, 'message'=>'请输入权限号']);
        }

        if (empty($args_data['permissionName'])) {
            return response()->json([ 'code'=>100002, 'message'=>'请输入权限名称']);
        }

        if ( !ebsig_is_int($args_data['permissionGroupID']) ) {
            return response()->json([ 'code'=>100003, 'message'=>'请选择权限组']);
        }

        if(!ebsig_is_int($args_data['secondLevelPermissionGroupID'])){
            return response()->json([ 'code'=>100004, 'message'=>'请选择权限组']);
        }

        if (empty($args_data['permissionRequest'])) {
            return response()->json([ 'code'=>100005, 'message'=>'请输入权限路径']);
        }

        //查询一级权限组信息
        $permission_group = PermissionGroup::find($args_data['permissionGroupID']);
        if (!$permission_group) {
            return response()->json([ 'code'=>100006, 'message'=>'权限组不存在']);
        } else {
            $permission_group = $permission_group->toArray();
        }

        //查询二级权限组信息
        $second_level_permission_group = PermissionGroup::find($args_data['secondLevelPermissionGroupID']);
        if (!$second_level_permission_group || $second_level_permission_group['parent_id'] != $args_data['permissionGroupID']) {
            return response()->json([ 'code'=>100007, 'message'=>'权限组不存在']);
        } else {
            $second_level_permission_group = $second_level_permission_group->toArray();
        }

        if($second_level_permission_group['min_permission_id'] > $args_data['permissionID'] || $second_level_permission_group['max_permission_id'] < $args_data['permissionID']){
            return response()->json([ 'code'=>100008, 'message'=>'输入的权限号不在所选的权限组范围内']);
        }

        if ( !empty( $args_data['uuid'] ) ) {

            $permission_obj = Permission::where('uuid' , $args_data['uuid'] )->first();
            if ( !$permission_obj ) {
                return response()->json([ 'code'=>100009, 'message'=>'权限不存在']);
            }

            if ( $permission_obj->permissionID != $args_data['permissionID']) {

                $exist = Permission::find($args_data['permissionID']);
                if ( $exist ) {
                    return response()->json([ 'code'=>100010, 'message'=>'权限号已存在']);
                }

            }

        } else {

            $exist = Permission::find($args_data['permissionID']);
            if ( $exist ) {
                return array( 'code' => 100010, 'message' => '权限号已存在' );
            }

            $permission_obj = new Permission();
            $permission_obj->uuid = makeUuid();
            $permission_obj->timeStamp = Carbon::now();
            $permission_obj->creator = Redis::get('CC_ADMIN_USER_ID' . session()->getId());
            $permission_obj->createTime = Carbon::now();

        }

        $permission_obj->permissionID = $args_data['permissionID'];
        $permission_obj->permissionName = $args_data['permissionName'];
        $permission_obj->permissionRequest = $args_data['permissionRequest'];
        $permission_obj->permissionGroupID = $args_data['permissionGroupID'];
        $permission_obj->permissionGroupName = $permission_group['group_name'];
        $permission_obj->secondLevelPermissionGroupID = $args_data['secondLevelPermissionGroupID'];
        $permission_obj->secondLevelPermissionGroupName = $second_level_permission_group['group_name'];

        $permission_obj->save();

        return response()->json(['code' => 200 , 'message' => 'ok' ]);
    }

}